<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

use App\Models\Admin\Permission;
use Request;


class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        // App\Model\Admin::class => App\Policies\AdminPolicy::class,
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();
        $pre = Request::segment(1);
        switch ($pre) {
            case 'admin':
                $this->admin();
                break;
        }
        
    }

    private function admin(){
        $permissions = Permission::with('roles')->get();
        foreach ($permissions as $permission) {
            Gate::define($permission->name, function ($user) use($permission) {
                return $user->hasPermission($permission);
            });
        }
    }
}
